$(document).ready(function () {
    $('.data-out-btn').click(function(){

        getHistorySpotDataAllCount({
            data:JSON.stringify({day:$('#vi-day-back-input').val()}),
            success:function(data){
                if(data.error_code == 0){
                    //console.log(data)
                    var _data = [['时间','外滩','南京路','豫园（外圈）','豫园（内圈）','田子坊','新天地']];
                    // var _data = [['时间','外滩','南京路','豫园（内圈）','田子坊','新天地']];
                    var _time = [];
                    var hour = -1;
                    data.everyHour.forEach(function(val){
                        var _hour = new Date(val.createTime).getHours();
                        if(hour !== _hour){
                            _time.push(_hour)
                            hour = _hour;
                        }
                    })
                    //console.log(_time)
                    var hour = -1;
                    var toHour = []
                    var line = [0,0,0,0,0,0]
                    data.everyHour.forEach(function(val,i){
                        var _hour = new Date(val.createTime).getHours();
                        var nowPopular = val.realtimeNumber === undefined ? '' :  val.realtimeNumber;
                        if(hour !== _hour){
                            if(_hour !== _time[0]){
                                //console.log(line)
                                toHour.push(line)
                                line = [0,0,0,0,0,0];
                            }
                            hour = _hour;
                        }
                        switch(val.spotName){
                            case '南京路':
                                
                                line[1] = nowPopular;
                                break;
                            case '南京路东拓':
                                break;
                            case '外滩':
                                line[0] = nowPopular;
                                break;
                            case '新天地':
                                line[5] = nowPopular;
                                break;
                            case '田子坊':
                                line[4] = nowPopular;
                                break;
                            case '豫园（内圈）':
                                line[3] = nowPopular;
                                break;
                            case '豫园（外圈）':
                                line[2] = nowPopular;
                                break;
                        }

                        if(i === (data.everyHour.length - 1)){
                            toHour.push(line)
                        }

                    })

                    //console.log(toHour)
                    toHour.map(function(val,i){
                        return val.unshift(_time[i] + '时')
                    })
                    toHour.forEach(function(val){
                        _data.push(val)
                    })

                    var fengzhi = ['峰值','','','','','','']
                    for(var key1 in data.maxTime){
                        var fengzhiItem = data.maxTime[key1];
                        switch(key1){
                            case '南京路':
                                fengzhi[2] = fengzhiItem;
                                break;
                            case '南京路东拓':
                                break;
                            case '外滩':
                                fengzhi[1] = fengzhiItem;
                                break;
                            case '新天地':
                                fengzhi[6] = fengzhiItem;
                                break;
                            case '田子坊':
                                fengzhi[5] = fengzhiItem;
                                break;
                            case '豫园（内圈）':
                                fengzhi[4] = fengzhiItem;
                                break;
                            case '豫园（外圈）':
                                fengzhi[3] = fengzhiItem;
                                break;
                        }
                    }
                    var total = ['截止时间累计值','','','','','','']
                    for(var key2 in data.total){
                        var totalItem = data.total[key2];
                        switch(key2){
                            case '南京路':
                                total[2] = totalItem;
                                break;
                            case '南京路东拓':
                                break;
                            case '外滩':
                                total[1] = totalItem;
                                break;
                            case '新天地':
                                total[6] = totalItem;
                                break;
                            case '田子坊':
                                total[5] = totalItem;
                                break;
                            case '豫园（内圈）':
                                total[4] = totalItem;
                                break;
                            case '豫园（外圈）':
                                total[3] = totalItem;
                                break;
                        }
                    }
                    var huanbi = ['环比','','','','','','']
                    for(var key3 in data.huanbi){
                        var huanbiItem = data.huanbi[key3];
                        switch(key3){
                            case '南京路':
                                huanbi[2] = huanbiItem;
                                break;
                            case '南京路东拓':
                                break;
                            case '外滩':
                                huanbi[1] = huanbiItem;
                                break;
                            case '新天地':
                                huanbi[6] = huanbiItem;
                                break;
                            case '田子坊':
                                huanbi[5] = huanbiItem;
                                break;
                            case '豫园（内圈）':
                                huanbi[4] = huanbiItem;
                                break;
                            case '豫园（外圈）':
                                huanbi[3] = huanbiItem;
                                break;
                        }
                    }

                    _data.push(fengzhi);
                    _data.push(total);
                    _data.push(huanbi);

                    //console.log(data)

                    const wb = XLSX.utils.book_new(); //创建一个新的工作簿
                    const ws = XLSX.utils.aoa_to_sheet(_data); //使用二维数组创建一个工作表对象
                    XLSX.utils.book_append_sheet(wb, ws, "Sheet1"); //向工作簿中追加一个工作表
                    XLSX.writeFile(wb, $('#vi-day-back-input').val() + ".xlsx");
                }
            }
        })
        
    })
})